package com.sl.biz.modular.process.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.dao.mapper.ProcessVersionMapper;
import com.sl.biz.modular.process.dao.service.ProcessVersionDaoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProcessVersionDaoServiceImpl extends ServiceImpl<ProcessVersionMapper, ProcessVersionDO> implements ProcessVersionDaoService {

    @Override
    public List<ProcessVersionDTO> queryList(ProcessVersionDO processVersionDO) {

        LambdaQueryWrapper<ProcessVersionDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(processVersionDO.getProcessId()),
                ProcessVersionDO::getProcessId, processVersionDO.getProcessId());

        wrapper.orderByDesc(ProcessVersionDO::getLatestFlag).orderByDesc(ProcessVersionDO::getCreateTime);
        List<ProcessVersionDO> list = baseMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, ProcessVersionDTO.class);
    }

    @Override
    public Boolean setStatusFlag(ProcessVersionDO assertUserUse) {
        LambdaUpdateChainWrapper<ProcessVersionDO> chainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
        chainWrapper.eq(ProcessVersionDO::getId, assertUserUse.getId());
        chainWrapper.set(ProcessVersionDO::getDisableFlag, assertUserUse.getDisableFlag());
        boolean ret = chainWrapper.update();
        return ret;
    }

    @Override
    public Boolean setLatestFlag(ProcessVersionDO assertUserUse) {
        LambdaUpdateChainWrapper<ProcessVersionDO> chainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
        chainWrapper.eq(ProcessVersionDO::getId, assertUserUse.getId());
        chainWrapper.set(ProcessVersionDO::getLatestFlag, assertUserUse.getLatestFlag());
        boolean ret = chainWrapper.update();
        return ret;
    }

    @Override
    public ProcessVersionDO queryByProcessIdAndVersion(String processId, String processVersion) {
        LambdaQueryWrapper<ProcessVersionDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProcessVersionDO::getProcessId, processId);
        wrapper.eq(ProcessVersionDO::getProcessVersion, processVersion);
        wrapper.orderByDesc(ProcessVersionDO::getLatestFlag).orderByDesc(ProcessVersionDO::getCreateTime);
        ProcessVersionDO list = baseMapper.selectOne(wrapper);
        return list;
    }

    @Override
    public Boolean removeByProcessIdAndVersion(String processId, String processVersion) {
        LambdaUpdateWrapper<ProcessVersionDO> chainWrapper = new LambdaUpdateWrapper<>();
        chainWrapper.eq(ProcessVersionDO::getProcessId, processId);
        chainWrapper.eq(ProcessVersionDO::getProcessVersion, processVersion);
        return super.remove(chainWrapper);
    }

}
